//
//  MenuCell.m
//  南京大牌档
//
//  Created by student on 14-1-9.
//  Copyright (c) 2014年 无锡第三团队. All rights reserved.
//

#import "MenuCell.h"

@implementation MenuCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        [self initSubview];
        
        
    }
    return self;
}

-(void)initSubview
{
    foodLeftView=[[UIView alloc]initWithFrame:CGRectZero];
    foodRightView=[[UIView alloc]initWithFrame:CGRectZero];
    foodLeftView.backgroundColor=[UIColor colorWithRed:255/255.0 green:215/255.0 blue:0/255.0 alpha:1];
    foodRightView.backgroundColor=[UIColor colorWithRed:255/255.0 green:215/255.0 blue:0/255.0 alpha:1];
   

    
    foodLeftImage=[[UIImageView alloc]initWithFrame:CGRectZero];
    _foodRightImage=[[UIImageView alloc]initWithFrame:CGRectZero];
    foodLeftImage.userInteractionEnabled=YES;
    _foodRightImage.userInteractionEnabled=YES;
    foodLeftImage.backgroundColor=[UIColor clearColor];
    _foodRightImage.backgroundColor=[UIColor clearColor];
    [foodLeftView addSubview:foodLeftImage];
    [foodRightView addSubview:_foodRightImage];
    
    titleLeft=[[UILabel alloc]initWithFrame:CGRectZero];
    titleRight=[[UILabel alloc]initWithFrame:CGRectZero];
    titleLeft.backgroundColor=[UIColor clearColor];
    titleRight.backgroundColor=[UIColor clearColor];
    titleLeft.font=[UIFont boldSystemFontOfSize:12];
    titleRight.font=[UIFont boldSystemFontOfSize:12];
    [self.contentView addSubview:titleLeft];
    [self.contentView addSubview:titleRight];
    
    
    orderLeft=[[UIButton alloc]initWithFrame:CGRectZero];
    _orderRight=[[UIButton alloc]initWithFrame:CGRectZero];
    
    [foodLeftImage addSubview:orderLeft];
    [_foodRightImage addSubview:_orderRight];
    
    
    priceLeft=[[UILabel alloc]initWithFrame:CGRectZero];
    priceRight=[[UILabel alloc]initWithFrame:CGRectZero];
    priceLeft.backgroundColor=[UIColor clearColor];
    priceRight.backgroundColor=[UIColor clearColor];
    priceLeft.font=[UIFont boldSystemFontOfSize:10];
    priceRight.font=[UIFont boldSystemFontOfSize:10];
    [self.contentView addSubview:priceLeft];
    [self.contentView addSubview:priceRight];
    
    showLeftNum=[[UILabel alloc]initWithFrame:CGRectZero];
    showRightNum=[[UILabel alloc]initWithFrame:CGRectZero];
    [foodLeftImage addSubview:showLeftNum];
    [_foodRightImage addSubview:showRightNum];
    
    [self.contentView addSubview:foodLeftView];
    [self.contentView addSubview:foodRightView];

}

-(void)translateCellData:(NSArray *)array
{
    NSArray  *translateArray=[NSArray arrayWithArray:array];
       NSDictionary *dic = @{@"Array":translateArray};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"translateCellData" object:self userInfo:dic];
}

-(void)showDetialFoodVC:(NSArray *)array
{
    NSArray  *showDetialFoodArray=[NSArray arrayWithArray:array];
    NSDictionary *dic = @{@"Array":showDetialFoodArray};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"showDetialFoodVC" object:self userInfo:dic];
}

-(void)orderButtonPressed:(UIButton *)button
{
    


    if (button==orderLeft) {
        
        [self translateCellData:_foodLeftArray];
        showLeftNum.frame=CGRectMake(0, 81, 50, 50);
        showLeftNum.textColor=[UIColor redColor];
        showLeftNum.backgroundColor=[UIColor clearColor];
        showLeftNum.text=@"+1";
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.8];
        showLeftNum.frame=CGRectMake(0, 60, 50, 50);
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(removeLabel)];
        [UIView commitAnimations];

    }
    else{
        
        [self translateCellData:_foodRightArray];
        showRightNum.frame=CGRectMake(0, 81, 50, 50);
        showRightNum.backgroundColor=[UIColor clearColor];
        showRightNum.textColor=[UIColor redColor];
        showRightNum.text=@"+1";
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.8];
        showRightNum.frame=CGRectMake(0, 60, 50, 50);
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(removeLabel)];
        [UIView commitAnimations];

    }

}

-(void)removeLabel
{
    showLeftNum.textColor=[UIColor clearColor];
    showRightNum.textColor=[UIColor clearColor];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    foodLeftView.frame=CGRectMake(6, 5, 151, 110);
    foodRightView.frame=CGRectMake(163, 5,151, 110);
    
    foodLeftImage.frame=CGRectMake(2, 2, 147, 106);
    _foodRightImage.frame=CGRectMake(2, 2, 147, 106);
    [foodLeftImage setImageWithURL:[NSURL URLWithString:_foodLeftString]];
    [_foodRightImage setImageWithURL:[NSURL URLWithString:_foodRightString]];
    
    tapFoodLeftImage = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                       action:@selector(handleSingleFingerEvent:)];
    UITapGestureRecognizer *tapFoodRightImage = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                   action:@selector(handleSingleFingerEvent:)];
    [foodLeftImage addGestureRecognizer:tapFoodLeftImage];
    [_foodRightImage addGestureRecognizer:tapFoodRightImage];
    
    titleLeft.frame=CGRectMake(6, 120, 100, 15);
    priceLeft.frame=CGRectMake(106, 120, 51, 15);
    
    titleRight.frame=CGRectMake(163, 120, 100, 15);
    priceRight.frame=CGRectMake(263, 120, 51, 15);
    
    titleLeft.text=_titleLeftString;
    titleRight.text=_titleRightString;
    
    priceLeft.text=_priceLeftString;
    priceRight.text=_priceRightString;
    
    orderLeft.frame=CGRectMake(122, 81, 25, 25);
    _orderRight.frame=CGRectMake(122, 81, 25, 25);
    [orderLeft setImage:[UIImage imageNamed:@"btn_add"] forState:UIControlStateNormal];
    [_orderRight setImage:[UIImage imageNamed:@"btn_add"] forState:UIControlStateNormal];
    

    

    
    [orderLeft addTarget:self action:@selector(orderButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [_orderRight addTarget:self action:@selector(orderButtonPressed:) forControlEvents:
        UIControlEventTouchUpInside];
}

- (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
{
    if (sender==tapFoodLeftImage) {
        [self showDetialFoodVC:_foodLeftArray];
    }
    else{
        [self showDetialFoodVC:_foodRightArray];
    }
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
